import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CountContainer extends InheritedWidget {

  //static CountContainer of(BuildContext context) => context.inheritFromWidgetOfExactType(CountContainer) as CountContainer;
  static CountContainer of(BuildContext context) => context.dependOnInheritedWidgetOfExactType<CountContainer>();


  final InheredDemoPageState state;
  final Function() increment;

  CountContainer({ Key key, @required this.state, @required this.increment, @required Widget child, }): super(key: key, child: child);
  // 判断是否需要更新
   @override
   bool updateShouldNotify(CountContainer oldWidget) => state.count != oldWidget.state.count;
}

class Counter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CountContainer container = CountContainer.of(context);
    // return Center(
    //   child: Text('tapped times ${container.state.count}'),
    // );

    return Scaffold(
      body: Text('tapped times ${container.state.count}'),
      floatingActionButton: FloatingActionButton(onPressed: container.increment),
    );
  }
}

class InheredDemoPage extends StatefulWidget {

    @override
    State<StatefulWidget> createState() {
     return InheredDemoPageState();
  }

}

class InheredDemoPageState extends State<InheredDemoPage> {


   int count = 0;
   void increment() => setState((){count++ ; });
  @override
  Widget build(BuildContext context) {
    return CountContainer(state: this, increment: increment, child: Counter());
  }

}